package entities;

import java.util.Date;

/**
 * 
 * @author eks691
 * 
 */
public class PaymentPlan {

	/** monthly payment amount */
	private double monthlyPayment;

	/** amount received so far by student */
	private double amountReceived;

	/** next payment date */
	private Date nextDate;

	/** next payment amount */
	private double nextAmount;

	/** prev payment date */
	private Date prevDate;

	/** prev payment amount */
	private double prevAmount;

	/** loan amount */
	private double loanAmount;

	/**
	 * Constructor
	 * 
	 * @param l
	 *            the amount of the student loan
	 */
	public PaymentPlan(double l) {
		loanAmount = l;
		monthlyPayment = loanAmount / 12;
		amountReceived = 0;
		nextDate = new Date(110, 11, 01);
		prevDate = null;
		nextAmount = 0.0;
		prevAmount = 0.0;
	}

	/**
	 * Gets the amount of the loan
	 * 
	 * @return amount of the loan
	 */
	public double getLoanAmount() {
		return loanAmount;
	}

	/**
	 * 
	 * @return the amount of the monthly payment to the student
	 */
	public double getMonthlyPayment() {
		return monthlyPayment;
	}

	/**
	 * 
	 * @return total amount received by the student so far
	 */
	public double getAmountReceived() {
		return amountReceived;
	}

	/**
	 * Sets the total amount received by the student so far
	 * 
	 * @param a
	 *            amount to add to the total amount received by student
	 */
	public void setAmountReceived(double a) {
		amountReceived = amountReceived + a;
	}

	/**
	 * Gets the next payment date of student
	 * 
	 * @return the next payment date of student
	 */
	public Date getNextDate() {
		return nextDate;
	}

	/**
	 * Gets the next date as a string
	 * 
	 * @return the nextDate as a string
	 */
	public String getNextDateAsString() {

		Date mydate = nextDate;
		String myDateString = "";

		if (nextDate != null) {
			myDateString += mydate.getYear() + 1900;

			int month = mydate.getMonth() + 1;
			if (month < 10) {
				myDateString += "0" + month + "/";
			} else {
				myDateString += month + "/";
			}

			if (mydate.getDate() < 10) {
				myDateString += "0" + mydate.getDate() + "/";
			} else {
				myDateString += mydate.getDate() + "/";
			}

			return myDateString;
		}

		return "";
	}

	/**
	 * Makes a date type out of string
	 * 
	 * @param d
	 *            the string to turn into a date
	 */
	public void setNextDateAsString(String d) {

		if (d.matches("[0-2][90][0-9][0-9]/[0-1][0-9]/[0-3][0-9]")) {
			Date mydate = new Date();
			mydate.setMonth(Integer.parseInt(d.substring(0, 2)) - 1);
			mydate.setDate(Integer.parseInt(d.substring(3, 5)));
			mydate.setYear(Integer.parseInt(d.substring(6, 10)) - 1900);
			setNextDate(mydate);
		}
	}

	/**
	 * Gets the Prev date as a string
	 * 
	 * @return the PrevDate as a string
	 */
	public String getPrevDateAsString() {

		Date mydate = prevDate;
		String myDateString = "";

		if (prevDate != null) {
			myDateString += mydate.getYear() + 1900;

			int month = mydate.getMonth() + 1;
			if (month < 10) {
				myDateString += "0" + month + "/";
			} else {
				myDateString += month + "/";
			}

			if (mydate.getDate() < 10) {
				myDateString += "0" + mydate.getDate() + "/";
			} else {
				myDateString += mydate.getDate() + "/";
			}

			return myDateString;
		}

		return "";
	}

	/**
	 * Makes a date type out of string
	 * 
	 * @param d
	 *            the string to turn into a date
	 */
	public void setPrevDateAsString(String d) {

		if (d.matches("[0-2][90][0-9][0-9]/[0-1][0-9]/[0-3][0-9]")) {

			Date mydate = new Date();
			mydate.setMonth(Integer.parseInt(d.substring(0, 2)) - 1);
			mydate.setDate(Integer.parseInt(d.substring(3, 5)));
			mydate.setYear(Integer.parseInt(d.substring(6, 10)) - 1900);
			setPrevDate(mydate);
		}
	}

	/**
	 * Sets the next payment date of student
	 * 
	 * @param d
	 *            the next payment date of student
	 */
	public void setNextDate(Date d) {
		nextDate = d;
	}

	/**
	 * Gets the previous payment date of student
	 * 
	 * @return prevDate
	 */
	public Date getPrevDate() {
		return prevDate;
	}

	/**
	 * Sets the previous payment date of student
	 * 
	 * @param d
	 *            previous payment date of student
	 */
	public void setPrevDate(Date d) {
		prevDate = d;
	}

	/**
	 * Gets the next payment amount for student
	 * 
	 * @return next payment amount for student
	 */
	public double getNextAmount() {
		return nextAmount;
	}

	/**
	 * Sets the next payment amount for student
	 * 
	 * @param a
	 *            amount to set the next payment to
	 */
	public void setNextAmount(Double a) {
		nextAmount = a;
	}

	/**
	 * Gets the previous payment amount for student
	 * 
	 * @return previous payment amount for student
	 */
	public double getPrevAmount() {
		return prevAmount;
	}

	/**
	 * Sets the amount of the previous payment
	 * 
	 * @param a
	 *            previous payment amount for student
	 */
	public void setPrevAmount(Double a) {
		prevAmount = a;
	}

	/**
	 * regression tests
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		PaymentPlan myPlan = new PaymentPlan(500.0);

		if (myPlan.getLoanAmount() != 500.0) {
			System.out.println("ERROR: Constructor");
		}

		if (myPlan.getMonthlyPayment() != myPlan.getLoanAmount() / 12) {
			System.out.println("ERROR: Monthly Payments");
		}

		myPlan.setAmountReceived(50.0);
		if (myPlan.getAmountReceived() != 50.0) {
			System.out.println("ERROR: Amount Received");
		}

		myPlan.setNextAmount(20.0);
		if (myPlan.getNextAmount() != 20.0) {
			System.out.println("ERROR: Next Amount");
		}

		myPlan.setPrevAmount(20.0);
		if (myPlan.getPrevAmount() != 20.0) {
			System.out.println("ERROR: Prev Amount");
		}

		myPlan.setNextDate(new Date(2010, 12, 1));
		if (myPlan.getNextDate().compareTo(new Date(2010, 12, 1)) != 0) {
			System.out.println("ERROR: Next Date");
		}

		myPlan.setPrevDate(new Date(2010, 12, 1));
		if (myPlan.getPrevDate().compareTo(new Date(2010, 12, 1)) != 0) {
			System.out.println("ERROR: Prev Date");
		}

	}

}
